package thread;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: volatile解决编译器优化问题
 * User: 王圆豪
 * Date: 2025-04-27
 * Time: 23:33
 */
public class Demo22 {
    private volatile static int flag = 0;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (flag == 0) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
            }
        });
        Thread t2 = new Thread(()->{
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个非零的数字：");
            flag = sc.nextInt();
        });
        t1.start();
        t2.start();
    }
}
